package com.hospital.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hospital.dao.UserInfoDAO;
import com.hospital.model.UserInfo;
import com.hospital.util.DateUtil;
import com.hospital.util.DbUtil;
import com.mysql.jdbc.Connection;

@WebServlet("/manage/OrderServlet")
public class OrderServlet extends HttpServlet {
	private static final String CONTENT_TYPE = "text/xml; charset=UTF-8";
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}
	protected void doPost(HttpServletRequest request,HttpServletResponse response) 
			throws ServletException, IOException {//挂号单信息管理
		String method = request.getParameter("method");
		if ("add".equals(method)) {
			String name = request.getParameter("name").trim();
			//找出病人id
			String sql0 = "select id from t_patient_info where account = ?";
			Connection conn = null;
			PreparedStatement stmt = null;
			ResultSet rs = null;
			conn = (Connection) DbUtil.getConnection();
			String patientId = null;
			try {
				stmt = conn.prepareStatement(sql0);
				stmt.setString(1, name);
				rs = stmt.executeQuery();
				while(rs.next()){
					patientId = rs.getString(1);
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			String age = request.getParameter("age").trim();
			String item = request.getParameter("item").trim();
			String type = request.getParameter("type").trim();
			String doctorName = request.getParameter("doctorName");
			String guahaoDeptId = request.getParameter("keshi").trim();
			String sql = "insert into t_order(oid, name, age, item, type, doctorName, guahaoDeptId, patientId, finish) " +
					"values('" + DateUtil.getDateTime("yyyyMMddHHmmss") + "', '" + name + "','" + age + "', '" + item + "', '" + type + "', '" + doctorName + "', '" + guahaoDeptId+ "', '" + patientId + "', '" +"no"+"')";
			DbUtil.add_modify(sql);
			
			String sql_money = "update t_patient_info set money = money - 10 where account = '" + name + "'";
			DbUtil.add_modify(sql_money);
			
			String sqlordermoney = "insert into t_order_money(oid, money, addtime) values('"
					+ DateUtil.getDateTime("yyyyMMddHHmmss") + "', '" + 10 + "','" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime()) + "')";
			DbUtil.add_modify(sqlordermoney);
			request.getRequestDispatcher("/manage/order_manage.jsp").forward(request, response);
		} else if("addByPatient".equals(method)){
			String name = request.getParameter("name").trim();
			//找出病人id
			String sql0 = "select id from t_patient_info where account = ?";
			Connection conn = null;
			PreparedStatement stmt = null;
			ResultSet rs = null;
			conn = (Connection) DbUtil.getConnection();
			String patientId = null;
			try {
				stmt = conn.prepareStatement(sql0);
				stmt.setString(1, name);
				rs = stmt.executeQuery();
				while(rs.next()){
					patientId = rs.getString(1);
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			String age = request.getParameter("age").trim();
			String item = request.getParameter("item").trim();
			String type = request.getParameter("type").trim();
			String doctorName = request.getParameter("doctorName");
			String guahaoDeptId = request.getParameter("keshi").trim();
			String sql = "insert into t_order(oid, name, age, item, type, doctorName, guahaoDeptId, patientId, finish) " +
					"values('" + DateUtil.getDateTime("yyyyMMddHHmmss") + "', '" + name + "','" + age + "', '" + item + "', '" + type + "', '" + doctorName + "', '" + guahaoDeptId+ "', '" + patientId + "', '" +"no"+"')";
			DbUtil.add_modify(sql);
			
			String sql_money = "update t_patient_info set money = money - 10 where account = '" + name + "'";
			DbUtil.add_modify(sql_money);
			
			String sqlordermoney = "insert into t_order_money(oid, money, addtime) values('"
					+ DateUtil.getDateTime("yyyyMMddHHmmss") + "', '" + 10 + "','" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime()) + "')";
			DbUtil.add_modify(sqlordermoney);
			request.getRequestDispatcher("/manage/patientorder_info_show.jsp").forward(request, response);
		} else if ("del".equals(method)) {
			String[] id = request.getParameterValues("selectFlag");
			DbUtil.del("t_order", id);
			request.getRequestDispatcher("/manage/order_manage.jsp").forward(request, response);
		} else if ("modify".equals(method)) {
			int id = Integer.parseInt(request.getParameter("id"));
			String name = request.getParameter("name").trim();
			String age = request.getParameter("age").trim();
			String guahaoDeptId = request.getParameter("guahaoDeptId").trim();
			String doctorName = request.getParameter("doctorName");
			String sql = "update t_order set name = '" + name + "', age = '" + age + "', guahaoDeptId = '" + guahaoDeptId + "', doctorName = '"+ doctorName +"' where id = " + id;
			DbUtil.add_modify(sql);
			request.getRequestDispatcher("/manage/order_manage.jsp").forward(request, response);
		}
	}
}